package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.risk;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.QuotaConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.StrategyConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * description:
 *
 * @author jackdaw
 * @date 2020-06-14 18:35
 */
@Component
public class Lbp030InsuranceRiskStrategy implements InsuranceRiskStrategy {
    private final InsuredCoreRepository insuredCoreRepository;

    private final RedisService redisService;

    public Lbp030InsuranceRiskStrategy(InsuredCoreRepository insuredCoreRepository, RedisService redisService) {
        this.insuredCoreRepository = insuredCoreRepository;
        this.redisService = redisService;
    }

    @Override
    public InsuredModel checkProductRiskQuota(ProductModel product, RequestModel requestModel) {
        //当前单保费
        Integer payYear = product.getPayYear();
        List<InsurantModel> insurantModelList = requestModel.getCustInfo().getInsurants().getInsurant();
        InsurantModel insurantModel = AcceptUtil.getInsurantModelBySeq(product.getProdInsSeq(), insurantModelList);
        Date valiDate = requestModel.getPolicyInfo().getCvalidate();
        int days = DateUtil.getDays(insurantModel.getBirthday(), valiDate);
        int years = DateUtil.getYears(insurantModel.getBirthday(), valiDate);
        if (StringUtils.equals(StrategyConstant.AGE, product.getPayYearFlag())) {
            payYear = payYear - years;
        }
        BigDecimal currentPrem = product.getPrem();
        if (!StringUtils.equals(requestModel.getProductInfo().getPayIntv(), UnderWriteConstant.SINGLE_PAY)) {
            currentPrem = product.getPrem().multiply(BigDecimal.valueOf(payYear)).multiply(BigDecimal.valueOf(12 / Integer.parseInt(requestModel.getProductInfo().getPayIntv())));
        }
        BigDecimal cachePrem = redisService.getCacheQuota(QuotaConstant.SORT_LBP030, insurantModel.getIdNo(), currentPrem, requestModel.getPolicyInfo().getContNo());
        BigDecimal historicalPrem = insuredCoreRepository.getHistoricalJbhlPrem(insurantModel.getInsuredNo());
        BigDecimal total = currentPrem.add(cachePrem).add(historicalPrem);
        if (days >= UnderWriteConstant.AGE_THIRTY_DAY && years <= UnderWriteConstant.AGE_SIXTY_YEAR && total.compareTo(BigDecimal.valueOf(5 * UnderWriteConstant.ONE_MILLION)) > 0) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                    String.format(UnderWriteConstant.UNDER_WRITE_NYGG0049_MESSAGE, insurantModel.getName(), years));
        } else if (years >= UnderWriteConstant.AGE_SIXTY_ONE_YEAR && years <= UnderWriteConstant.AGE_SIXTY_FIVE_YEAR && total.compareTo(BigDecimal.valueOf(2.5 * UnderWriteConstant.ONE_MILLION)) > 0) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                    String.format(UnderWriteConstant.UNDER_WRITE_NYGG0050_MESSAGE, insurantModel.getName(), years));
        }
        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);
    }
}
